<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/
defined ( '_JEXEC' ) or die ();

jimport ( 'joomla.application.component.view' );

class JAJobBoardViewJATransactions extends JAView {
	function display() {
		$task = JRequest::getVar ( 'task' );
		$layout = JRequest::getVar ( 'layout' );
		switch ($layout) {
			case 'element_user' :
				$this->element_user ();
				break;
			case 'element_product' :
				$this->element_product ();
				break;
			case 'form' :
				$this->displayForm ();
				break;
			case 'view' :
				$this->displayInvoice ();
				break;
			case 'trans_detail' :
				$this->displayTrans_View ();
				break;
			default :
				$this->displayListItems ();
		}
	
	}
	
	function displayTrans_View() {
		global $jbconfig;
		global $mainframe, $option, $permission;
		$paymentObject = get_payment_gateways ();
		$item = $this->get ( 'Item' );
		for($i = 0; $i < count ( $paymentObject ); $i ++)
			if ($paymentObject [$i]->id == $item->payment_method)
				$item->payment_method = $paymentObject [$i]->alias;
			//Payment method
		

		$arr_posting_plan_type = JobBoardConstant::getPostingPlan_Type ();
		
		//Invoice status
		$arr_status = JobBoardConstant::getStatus_Invoice ();
		$arr_product_type = array ("S" => "Subscription", "P" => "Posting Plan", "C" => "Credit" );
		$this->setLayout ( 'trans_detail' );
		;
		$this->assignRef ( 'config', $jbconfig );
		$this->assignRef ( 'status', $objStatus );
		$this->assignRef ( 'value_type', $arr_posting_plan_type );
		$this->assignRef ( 'product_type', $arr_product_type );
		$this->assignRef ( 'paysys', $paymentObject );
		$this->assignRef ( 'item', $item );
		
		parent::display ();
	}
	
	function displayInvoice() {
		global $jbconfig;
		$item = $this->get ( 'Item' );
		$customerDetail = getCustomerProfile ( $item->user_id );
		$this->assignRef ( 'customerDetail', $customerDetail );
		$this->assignRef ( 'config', $jbconfig );
		$this->assignRef ( 'item', $item );
		parent::display ();
	}
	
	function displayListItems() {
		global $mainframe, $option;
		$items = $this->get ( 'Items' );
		$filter_status = JRequest::getVar ( 'filter_status' );
		$option_1 = $option . '.jatransactions';
		$search = $mainframe->getUserStateFromRequest ( "$option_1.search", 'search', '', 'string' );
		$lists ['search'] = JString::strtolower ( $search );
		$lists ['order'] = $mainframe->getUserStateFromRequest ( $option_1 . '.filter_order', 'filter_order', 'a.created_date', 'cmd' );
		$lists ['order_Dir'] = $mainframe->getUserStateFromRequest ( $option_1 . '.filter_order_Dir', 'filter_order_Dir', '', 'word' );
		$lists ['status'] = JString::strtolower ( $filter_status );
		
		$pagination = $this->get ( 'Pagination' );
		$status = array ();
		$status [] = JHTML::_ ( 'select.option', '', '----' . JText::_ ( 'Select status' ) . '----' );
		$arr_status = JobBoardConstant::getStatus_Invoice ();
		
		{
			foreach ( $arr_status as $value => $text )
				$status [] = JHTML::_ ( 'select.option', $value, JText::_ ( $text ) );
			$objStatus = JHTML::_ ( 'select.genericlist', $status, 'filter_status', 'class="inputbox" size="1" onchange="submitform( );"', 'value', 'text', $filter_status );
		}
		
		$this->assignRef ( 'lists', $lists );
		$this->assignRef ( 'arr_payment_method', $paymentObject );
		$this->assignRef ( 'filter_status', $objStatus );
		$this->assignRef ( 'pagination', $pagination );
		$this->assignRef ( 'items', $items );
		parent::display ();
	}
	
	function displayForm() {
		global $jbconfig;
		global $mainframe, $option, $permission;
		$user = JFactory::getUser ();
				
		$user_permission = getPermissionByUserID ( $user->id );
				
		if( $user_permission > 0){
			if ($user_permission && ($user_permission & $permission ['admin'] ['viewtransactions']) == $permission ['admin'] ['viewtransactions']) {
				JResponse::setHeader ( 'HTTP/1.0 403', true );
				JError::raiseWarning ( 403, JText::_ ( 'Access denied.You don\'t have permission' ) );
				echo '<br /><a class="button" href="#" onclick="window.history.go(-1);">' . JText::_ ( 'Back' ) . '</a>';
				return $mainframe->redirect ( JRoute::_ ( "index.php?option=$option&view=jatransactions" ) );
			}
		}
		
		$paymentObject = get_payment_gateways ();
		$item = $this->get ( 'Item' );
		
		//Payment method
		if ($paymentObject) {
			$paysys = JHTML::_ ( 'select.genericlist', $paymentObject, 'payment_method', 'class="inputbox" size="1" style="width:220px"', 'id', 'alias', $item->payment_method );
		}
		
		$arr_posting_plan_type = JobBoardConstant::getPostingPlan_Type ();
		if ($arr_posting_plan_type) {
			foreach ( $arr_posting_plan_type as $value => $text )
				$posting_plan_type [] = JHTML::_ ( 'select.option', $value, JText::_ ( $text ) );
			$value_type = JHTML::_ ( 'select.genericlist', $posting_plan_type, 'value_type', 'class="inputbox" size="1"', 'value', 'text', $item->value_type );
		}
		//Invoice status
		$arr_status = JobBoardConstant::getStatus_Invoice ();
		if ($arr_status) {
			foreach ( $arr_status as $value => $text )
				$status [] = JHTML::_ ( 'select.option', $value, JText::_ ( $text ) );
			$objStatus = JHTML::_ ( 'select.genericlist', $status, 'completed', 'class="inputbox" size="1"', 'value', 'text', $item->completed );
		}
		if ($item->product_type != 'S') {
			$disable = $item->id ? 'disabled="disabled"' : '';
			$arr_product_type = array ("P" => "Posting Plan", "C" => "Credit" );
			foreach ( $arr_product_type as $value => $text )
				$product_type [] = JHTML::_ ( 'select.option', $value, JText::_ ( $text ) );
			$objproduct_type = JHTML::_ ( 'select.genericlist', $product_type, 'product_type', $disable . ' onchange="change_product_type(this.value)" id="product_type" class="inputbox" size="1"', 'value', 'text', $item->product_type );
		} else
			$objproduct_type = JText::_ ( 'Subscriptions' );
		
		JRequest::setVar ( 'product_type', $item->product_type );
		$objproductid = $this->show_dropdown_produt_list ( $item->product_id );
		$this->assignRef ( 'objproductid', $objproductid );
		$this->setLayout ( 'form' );
		;
		$this->assignRef ( 'config', $jbconfig );
		$this->assignRef ( 'status', $objStatus );
		$this->assignRef ( 'value_type', $value_type );
		$this->assignRef ( 'product_type', $objproduct_type );
		$this->assignRef ( 'paysys', $paysys );
		$this->assignRef ( 'item', $item );
		
		parent::display ();
	}
	
	function element_user() {
		global $mainframe;
		$element_user = $this->get ( 'UserList' );
		$this->setLayout ( 'element_user' );
		$this->assignRef ( 'items', $element_user );
		
		$search = $mainframe->getUserStateFromRequest ( JBCOMNAME . ".jaemployers.search", 'user_search', '', 'string' );
		$search = trim ( JString::strtolower ( $search ) );
		$this->assignRef ( 'search', $search );
		parent::display ();
	}
	
	function element_product() {
		$element_product = $this->get ( 'Products' );
		$this->setLayout ( 'element_product' );
		$this->assignRef ( 'items', $element_product );
		$this->assignRef ( 'search', JRequest::getVar ( 'product_search' ) );
		parent::display ();
	}
	
	function show_dropdown_produt_list($selected = '') {
		global $jbconfig;
		$db = JFactory::getDBO ();
		$product_type = JRequest::getVar ( 'product_type' );
		switch ($product_type) {
			case 'S' :
				$table_name = '#__ja_subscriptions';
				break;
			case 'C' :
				$table_name = '#__ja_credits';
				break;
			default :
				$table_name = '#__ja_posting_plans';
				break;
		}
		
		$query = "SELECT * FROM $table_name WHERE published=1";
		$db->setQuery ( $query );
		$result = $db->loadObjectList ();
		$product_option [] = JHTML::_ ( 'select.option', '', '-------------------' . JText::_ ( 'Select' ) . '-------------------' );
		if (count ( $result ) > 0) {
			for($i = 0; $i < count ( $result ); $i ++) {
				$product_option [] = JHTML::_ ( 'select.option', $result [$i]->id, $result [$i]->title );
			}
		}
		$disable = $product_type == 'S' ? 'disabled="disabled"' : '';
		$product_object = JHTML::_ ( 'select.genericlist', $product_option, 'product_id', $disable . ' onchange="change_product_id(this.value)" id="product_id" class="inputbox" size="1"', 'value', 'text', $selected );
		return $product_object;
	
	}
	
	function menu() {
		
		$task = JRequest::getVar ( 'task', 'user' );
		$userid = JRequest::getVar ( 'userid' );
		
		?>
<div class="t">
<div class="t">
<div class="t"></div>
</div>
</div>
<div class="m">
<ul id="submenu">
	<li><a
		href="index.php?option=com_jajobboard&view=jaemployers&task=user&cid[]=<?php
		echo $userid;
		?>"
		class="<?php
		if ($task == null || $task == 'user')
			echo 'active'?>">
							<?php
		echo JText::_ ( 'User Details' );
		?>
						</a></li>
	<li><a
		href="index.php?option=com_jajobboard&view=jaemployers&task=account&cid[]=<?php
		echo $userid;
		?>"
		class="<?php
		if ($task == 'account')
			echo 'active'?>">
							<?php
		echo JText::_ ( 'Account Details' );
		?>
						</a></li>
	<li><a
		href="index.php?option=com_jajobboard&view=jaemployers&task=profile&cid[]=<?php
		echo $userid;
		?>"
		class="<?php
		if ($task == 'profile')
			echo 'active'?>">
								<?php
		echo JText::_ ( 'Profile Details' );
		?>
							</a></li>
	<li><a
		href="index.php?option=com_jajobboard&view=jatransactions&task=trans_viewbyuser&userid=<?php
		echo $userid;
		?>"
		class="<?php
		if ($task == 'trans_viewbyuser')
			echo 'active'?>">
							<?php
		echo JText::_ ( 'Transactions' );
		?>
						</a></li>

	<!--<li>
						<a href="index.php?option=com_jajobboard&view=jaemployers&task=invoices&cid[]=<?php
		echo $userid;
		?>" class="<?php
		if ($task == 'invoices')
			echo 'active'?>">
							<?php
		echo JText::_ ( 'Invoices' );
		?>
						</a>
					</li>-->
	<!--					<li>
						<a href="index.php?option=com_jajobboard&view=japostingplans_invoice&task=plan_viewbyuser&userid=<?php
		echo $userid;
		?>" class="<?php
		if ($task == 'plan_viewbyuser')
			echo 'active'?>">
							<?php
		echo JText::_ ( 'Posting Plan Invoice' );
		?>
						</a>
					</li>
					<li>
						<a href="index.php?option=com_jajobboard&view=jacredits_invoice&task=credit_viewbyuser&userid=<?php
		echo $userid;
		?>" class="<?php
		if ($task == 'credit_viewbyuser')
			echo 'active'?>">
							<?php
		echo JText::_ ( 'Creadit Invoice' );
		?>
						</a>
					</li>																			-->
</ul>
<div class="clr"></div>
</div>
<div class="b">
<div class="b">
<div class="b"></div>
</div>
</div>
<?php
	}
}
